﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Cashier_Interface;
using Cashier_Service;
using Cashier_Model;
using Cashier_Utils;
using Cashier.utils;

namespace Cashier.member
{
    public partial class MemberMoneyAddForm : Form
    {
        private MainForm mainform;
        private IReloadData iReloadData;
        private MemberService memberService;
        private MemberMoneyService memberMoneyService;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mainform"></param>
        /// <param name="iReloadData"></param>
        /// <param name="memberno"></param>
        public MemberMoneyAddForm(MainForm mainform, IReloadData iReloadData, String memberno)
        {
            InitializeComponent();
            this.mainform = mainform;
            this.iReloadData = iReloadData;
            this.memberService = new MemberService();
            this.memberMoneyService = new MemberMoneyService();

            //设置权限
            Button[] buttons = { button_revoke };
            RightsforControls.RightsforButton(mainform.Admin.RolePurviewtable, buttons);

            this.InitialFormData(memberno);
            textbox_memberno.LostFocus += new EventHandler(textbox_memberno_LostFocus);
        }

        /// <summary>
        /// 初始化表单数据
        /// </summary>
        /// <param name="memberno"></param>
        private void InitialFormData(String memberno)
        {
            if (!String.IsNullOrEmpty(memberno))
            {
                Member member = this.memberService.GetMember(0, memberno);
                if (member != null)
                {
                    textbox_memberno.Text = member.Memberno;
                    label_name.Text = member.Name;
                    label_lastMoney.Text = member.Balance.ToString("f2");

                    textbox_money.Text = "0.00";
                    textbox_note.Text = String.Empty;
                }
            }
        }

        /// <summary>
        /// 会员编号失去焦点事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void textbox_memberno_LostFocus(object sender, EventArgs e)
        {
            String memberno = textbox_memberno.Text.Trim();
            this.InitialFormData(memberno);
        }

        /// <summary>
        /// 保存扣款信息
        /// </summary>
        private void SaveMoney()
        {
            try
            {
                String memberno = textbox_memberno.Text.Trim();
                String name = label_name.Text;
                Decimal lastMoney = Decimal.Parse(label_lastMoney.Text);
                Decimal money = Decimal.Parse(textbox_money.Text.Trim());
                String note = textbox_note.Text.Trim();

                if (String.IsNullOrEmpty(memberno))
                {
                    MessageBox.Show("会员编号不能为空！");
                    return;
                }
                if (money <= 0)
                {
                    MessageBox.Show("扣款金额必须大于0！");
                    return;
                }
                if (money > lastMoney)
                {
                    MessageBox.Show("扣款金额不得超过卡内金额！");
                    return;
                }

                Member member = memberService.GetMember(0, memberno);
                if (member == null || !member.Name.Equals(name))
                {
                    MessageBox.Show("会员编号有误！");
                    return;
                }

                Admin admin = mainform.Admin;
                MemberMoney memberMoney = new MemberMoney();
                memberMoney.EnterpriseNo = admin.EnterpriseNo;
                memberMoney.StoreNo = admin.StoreNo;
                memberMoney.TransNo = GuidUtil.GetGuidToLong().ToString();
                memberMoney.MemberNo = memberno;
                memberMoney.Money = money;
                memberMoney.LastMoney = lastMoney - money;
                memberMoney.Note = note;
                memberMoney.MoneyTime = DateTime.Now;
                memberMoney.AdminUsername = admin.Username;
                memberMoney.AdminName = admin.Name;
                memberMoney.CreateTime = DateTime.Now;
                long id = memberMoneyService.InsertMemberMoney(memberMoney);
                if (id > 0)
                {
                    MessageBox.Show("扣款成功！");
                    this.iReloadData.ReloadDataGridViewData(null);
                    this.ClearFormData();
                    this.Close();
                }
                else
                {
                    MessageBox.Show("扣款失败！");
                }
            }
            catch (Exception ex)
            {
                LogsUtil.Record(ex);
            }
        }

        /// <summary>
        /// 清空表单数据
        /// </summary>
        private void ClearFormData()
        {
            textbox_memberno.Text = String.Empty;
            label_name.Text = String.Empty;
            label_lastMoney.Text = "0.00";
            textbox_money.Text = "0.00";
            textbox_note.Text = String.Empty;
        }

        /// <summary>
        /// 扣款事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_revoke_Click(object sender, EventArgs e)
        {
            this.SaveMoney();
        }

        /// <summary>
        /// 退出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_exit_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(label_name.Text.Trim()))
            {
                DialogResult result = MessageBox.Show("确定要退出吗？", "提示", MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes)
                {
                    this.Close();
                }
            }
            else
            {
                this.Close();
            }
        }
    }
}
